package com.duanzz.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

/**
 * 执行消耗时间插件
 * @author duanzz
 * @description
 * @date 2021/1/7
 */
@Intercepts(
        @Signature(
                type = Executor.class,method = "query",args = {
                MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class
                }
        )
)
public class ExecuteCostTimePlugin implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 前置增强
        long t1 = System.currentTimeMillis();
        Object proceed = invocation.proceed();
        // 后置增强
        long t2 = System.currentTimeMillis();
        System.out.println("Executor执行查询的时间为 " + ((t2-t1)/1000.0) +" 毫秒");
        return proceed;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
